const {
  GiftShopModel
} = require('../../../../utils/giftshop-model.js');
var giftShopModel = new GiftShopModel();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cityShow: false,
    provincesShow: false,
    provinceName: '', //省
    provinceCode: '', //省
    cityName: '', //市
    cityCode: '', //市
    districtName: '', //区
    districtCode: '', //区
    streetName: '', //街道
    streetCode: '', //街道
    iscustomTag: false, //是否显示自定义标签
    iscustomTagStatus: false, //自定义标签样式
    customTag: '', //输入框的内容
    addressTag: '', //自定义标签内容
    receiverName: '', //收货人姓名
    receiverPhone: '', //收货人电话
    fullAddress: '', //详细地址
    isDefault: false, //0否 1:是
    disabled: true, //按钮是否能点击
    memberAddressId: '', //地址id
    focus: false, //标签聚焦
    addressData: {}, //省市区选择传参
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      memberAddressId: options.id || ''
    })
    let barTitle = ''
    if (options.id && options.id != '') {
      barTitle = "编辑收货地址"
      this.getAddressDetail()
    } else {
      barTitle = "新建收货地址"
    }
    wx.setNavigationBarTitle({
      title: barTitle
    })
  },

  /**
   * 地址详情
   */
  getAddressDetail: function () {
    var param = {
      merchantId: wx.getStorageSync('merchantId'), //商户ID
      memberAddressId: this.data.memberAddressId //用户地址ID
    }
    giftShopModel.addressDetail(param).then(data => {
      if (data) {
        this.setData({
          provinceName: data.provinceName, //省
          provinceCode: data.provinceCode, //省
          cityName: data.cityName, //市
          cityCode: data.cityCode, //市
          districtName: data.districtName, //区
          districtCode: data.districtCode, //区
          streetName: data.streetName ? data.streetName : '', //街道
          streetCode: data.streetCode ? data.streetCode : '', //街道
          iscustomTag: (data.addressTag && data.addressTag != '家' && data.addressTag != '公司' && data.addressTag != '学校') ? true : false, //是否显示自定义标签
          iscustomTagStatus: (data.addressTag != '' && data.addressTag != '家' && data.addressTag != '公司' && data.addressTag != '学校') ? true : false, //是否显示自定义标签
          customTag: (data.addressTag && data.addressTag != '家' && data.addressTag != '公司' && data.addressTag != '学校') ? data.addressTag : '',
          focus: (data.addressTag && data.addressTag != '家' && data.addressTag != '公司' && data.addressTag != '学校') ? false : true, //标签聚焦
          addressTag: data.addressTag, //自定义标签内容
          receiverName: data.receiverName, //收货人姓名
          receiverPhone: data.receiverPhone, //收货人电话
          fullAddress: data.fullAddress, //详细地址
          isDefault: data.isDefault == 1 ? true : false, //0否 1:是
          disabled: false, //按钮是否能点击
        })
      }
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none',
        duration: 2000
      })
    })
  },

  /**
   * 选择城市
   */
  getCity: function (e) {
    wx.reportAnalytics('gift_address_selectcity');
    this.setData({
      provinceName: e.detail.provinceName, //省
      provinceCode: e.detail.provinceCode, //省
      cityName: e.detail.cityName, //省
      cityCode: e.detail.cityCode, //省
      districtName: e.detail.districtName, //区
      districtCode: e.detail.districtCode, //区
      streetName: e.detail.streetName, //街道
      streetCode: e.detail.streetCode //街道
    })
  },
  /**
   * 保存
   */
  saveAddress: function () {
    wx.reportAnalytics('gift_address_save');
    var param = {
      merchantId: wx.getStorageSync('merchantId'), //商户ID
      memberAddressId: this.data.memberAddressId, //用户地址ID
      provinceCode: this.data.provinceCode, //省编码
      provinceName: this.data.provinceName, //省名称
      cityCode: this.data.cityCode, //市编码
      cityName: this.data.cityName, //市名称
      districtCode: this.data.districtCode, //区编码
      districtName: this.data.districtName, //区名称
      streetCode: this.data.streetCode, //街道编码
      streetName: this.data.streetName, //街道名称
      fullAddress: this.data.fullAddress, //详细地址
      receiverName: this.data.receiverName, //收件人
      receiverPhone: this.data.receiverPhone, //收件人电话
      addressTag: this.data.addressTag, //地址标签
      isDefault: this.data.isDefault ? 1 : 0, //0否 1:是
    }
    giftShopModel.addressSave(param).then(data => {
      if (param.memberAddressId == "") {
        wx.showToast({
          title: '地址添加成功',
          icon: 'success',
          duration: 2000
        })
      } else {
        wx.showToast({
          title: '地址修改成功',
          icon: 'success',
          duration: 2000
        })
      }
      setTimeout(function () {
        wx.navigateBack({
          delta: 1
        })
      }, 1500)
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none',
        duration: 2000
      })
    })
  },
  /**
   * 选择所在地区,省市区code一定要重新赋值，不重新赋值可能会捕获不到
   */
  selectCity: function () {
    let addressInfo={
      provinceCode: this.data.provinceCode, //省编码
      provinceName: this.data.provinceName, //省名称
      cityCode: this.data.cityCode, //市编码
      cityName: this.data.cityName, //市名称
      districtCode: this.data.districtCode, //区编码
      districtName: this.data.districtName, //区名称
      streetCode: this.data.streetCode, //街道编码
      streetName: this.data.streetName, //街道名称
      cityShow: true
    }
    this.setData({
      addressData:addressInfo
    })
  },
  /**
   * 收货人
   */
  setReceiverName(e) {
    var name = /[^A-Za-z0-9\u4e00-\u9fa5]/g;
    var value = e.detail.value;
    if (name.test(value)) {
      this.setData({
        receiverName: this.data.receiverName
      })
    } else {
      this.setData({
        receiverName: value
      })
    }
    this.checkInfo();
  },

  /**
   * 设置标签
   */
  setTag(e) {
    wx.reportAnalytics('gift_address_selecttag');
    let tag = e.currentTarget.dataset.name
    this.setData({
      customTag: '',
      addressTag: tag,
      iscustomTag: false
    })
    this.checkInfo();
  },

  /**
   * 手机号
   */
  setReceiverPhone(e) {
    var value = e.detail.value;
    this.setData({
      receiverPhone: value
    })
    this.checkInfo();
  },

  /**
   * 详细地址
   */
  setFullAddress(e) {
    var value = e.detail.value;
    this.setData({
      fullAddress: value
    })
    this.checkInfo();
  },

  /**
   * 自定义便签
   */
  setCustomTag: function () {
    wx.reportAnalytics('gift_address_customtag');
    if (this.data.customTag == '' || this.data.customTag == '家' || this.data.customTag == '公司' || this.data.customTag == '学校') {
      this.setData({
        customTag: '',
        focus: true,
        iscustomTag: true,
        iscustomTagStatus: false
      })
    } else {
      this.setData({
        focus: true,
        iscustomTag: false,
        iscustomTagStatus: true
      })
    }
  },

  /**
   * 自定义便签的内容
   */
  blurAddressTag: function (e) {
    if (e.detail.value == "" || e.detail.value == '家' || e.detail.value == '公司' || e.detail.value == '学校') {
      this.setData({
        addressTag: e.detail.value ? e.detail.value : this.data.addressTag,
        iscustomTag: false,
        iscustomTagStatus: false,
        focus: false,
      })
    } else {
      this.setData({
        addressTag: e.detail.value,
        iscustomTag: true,
        iscustomTagStatus: true,
        focus: false
      })
    }
  },

  /**
   * 聚焦时触发
   */
  focusAddressTag: function () {
    this.setData({
      iscustomTagStatus: false,
    })
  },

  /**
   * 标签
   */
  setAddressTag(e) {
    this.setData({
      iscustomTagStatus: false,
      focus: true,
    })
    var name = /[^A-Za-z\u4e00-\u9fa5]/g;
    var value = e.detail.value;
    if (name.test(value)) {
      this.setData({
        customTag: this.data.customTag,
      })
    } else {
      this.setData({
        customTag: value,
      })
    }
  },

  /**
   * 保存标签
   */
  saveAddressTag: function (e) {
    wx.reportAnalytics('gift_address_savetag');
    let that = this;
    let type = e.currentTarget.dataset.type //当前索引
    that.setData({
      iscustomTagStatus: type == 1 ? true : false,
      focus: type == 1 ? false : true,
      addressTag: that.data.customTag ? that.data.customTag : that.data.addressTag
    })
  },

  /**
   * 是否为默认值
   */
  switchChange(e) {
    wx.reportAnalytics('gift_address_isdefault');
    var value = e.detail.value;
    this.setData({
      isDefault: value
    })
  },
  /**
   * 验证输入的内容
   */
  checkInfo: function () {
    var that = this;
    var res = /^1\d{10}$/;
    var name = /[^A-Za-z0-9\u4e00-\u9fa5]/g;
    if (that.data.receiverName != '' && !name.test(that.data.receiverName) && that.data.receiverPhone != "" && that.data.receiverPhone.length == 11 && res.test(that.data.receiverPhone) && that.data.provinceCode != '' && that.data.cityCode != '' && that.data.fullAddress != '') {
      that.setData({
        disabled: false,
      })
    } else {
      that.setData({
        disabled: true,
      })
    }
  },
})